﻿using System;
using System.Windows.Forms;

namespace BuildHelper.Setup.Serwery
{
    public partial class SerweryForm : MyBaseSqlCeForm
    {
        public SerweryForm()
        {
            InitializeComponent();
        }
        public SerweryForm(object id)
        {
            InitializeComponent();
            this.CurrentRecord = id;
            xtraTabControl1.SetTabMinSize(200);
        }
        private void sTP_SRW_TYPLookUpEdit_EditValueChanged(object sender, EventArgs e)
        {
            if (MainRow == null)
                return;

            string str = sTP_SRW_TYPLookUpEdit.EditValue.AsString();
            UpdateSrvType(str);
        }

        private void UpdateSrvType(String type)
        {
            bool isJboss = type == GlobalTools.ServerTools.ServerType.JBoss4
                   || type == GlobalTools.ServerTools.ServerType.JBoss5;
            bool isOas = type == GlobalTools.ServerTools.ServerType.OAS;
           
            if (modeProvider1.CurrentMode != ProgNetComponentsDx.Enums.FormMode.Read)
            {
                string jbossTemplate = @"<?xml version='1.0' encoding='UTF-8'?>
<datasources>
    <local-tx-datasource>
        <jndi-name>[JNDI_NAME]</jndi-name>
            <connection-url>jdbc:oracle:thin:@[URL]</connection-url>
            <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
            <user-name>[USER]</user-name>
            <password>[PASS]</password>
            <min-pool-size>5</min-pool-size>
            <max-pool-size>10</max-pool-size>
        <idle-timeout-minutes>5</idle-timeout-minutes>
    </local-tx-datasource>
</datasources>".Replace("'", "\"");

                sTP_SRW_DS_JNDITextEdit.EditValue = "com.scc.gcm";
                switch(type){
                    case GlobalTools.ServerTools.ServerType.JBoss4:
                    case GlobalTools.ServerTools.ServerType.JBoss5:
                        sTP_SRW_DS_TEMPLATEMemoEdit.EditValue = jbossTemplate;
                    sTP_SRW_DEPLOY_NAZWATextEdit.EditValue = DBNull.Value;
                    sTP_SRW_HASLOTextEdit.EditValue = DBNull.Value;
                    sTP_SRW_LOGINTextEdit.EditValue = DBNull.Value;
                    sTP_SRW_LOGS_DIRTextEdit.EditValue = "${jboss.server.home.dir}/log";
                    sTP_SRW_START_BATFileDialogEdit.EditValue = DBNull.Value;
                    sTP_SRW_DS_NAZWATextEdit.EditValue = DBNull.Value;
                    teInstancja.Text = "default";
                    tePort.EditValue = "8080";
                        break;
                    case GlobalTools.ServerTools.ServerType.OAS:
                        sTP_SRW_DS_TEMPLATEMemoEdit.EditValue = DBNull.Value;
                    sTP_SRW_DEPLOY_NAZWATextEdit.EditValue = "GeneticSuite";
                    sTP_SRW_HASLOTextEdit.EditValue = "123";
                    sTP_SRW_LOGINTextEdit.EditValue = "oc4jadmin";
                    sTP_SRW_LOGS_DIRTextEdit.EditValue = "${oracle.j2ee.home}/log";
                    sTP_SRW_START_BATFileDialogEdit.EditValue = DBNull.Value;
                    sTP_SRW_DS_NAZWATextEdit.EditValue = "com.scc.gcm";
                    tePort.EditValue = "8888";
                    teInstancja.EditValue = DBNull.Value;
                        break;
                    case GlobalTools.ServerTools.ServerType.WebLogic:
                        sTP_SRW_LOGS_DIRTextEdit.EditValue = "$$$${user.dir}/servers/${weblogic.Name}/logs";
                        break;
                }
            }
            tabJboss.PageVisible = isJboss;
            tabOas.PageVisible = isOas;

            if (isJboss)
            {
                foreach (Control c in tabJboss.Controls)
                {
                    if (modeProvider1.ContainsSettingsFor(c))
                        modeProvider1.RefreshObject(c);
                }
            }
            if (isOas)
            {
                foreach (Control c in tabOas.Controls)
                {
                    if (modeProvider1.ContainsSettingsFor(c))
                        modeProvider1.RefreshObject(c);
                }
            }
        }
        protected override bool NewRecord()
        {
            bool res = base.NewRecord();
            if (res)
                sTP_SRW_TYPLookUpEdit.EditValue = GlobalTools.ServerTools.ServerType.JBoss5;
            UpdateSrvType(GlobalTools.ServerTools.ServerType.JBoss5);
            return res;
        }
        protected override void CopyRecord()
        {
            base.CopyRecord();
            this.sTP_SRW_NAZWATextEdit.EditValue = DBNull.Value;
        }
        private void modeProvider1_CustomGetModes(object sender, ProgNetComponentsDx.Mode.CustomModeSettingsEventArgs e)
        {
            if (sender is Control)
            {
                if (tabOas.Controls.Contains(sender as Control))
                {
                   e.IsRequired = sTP_SRW_TYPLookUpEdit.EditValue.AsString() == GlobalTools.ServerTools.ServerType.OAS;
                }
                if (tabJboss.Controls.Contains(sender as Control))
                {
                   e.IsRequired = sTP_SRW_TYPLookUpEdit.EditValue.AsString() == GlobalTools.ServerTools.ServerType.JBoss4 || sTP_SRW_TYPLookUpEdit.EditValue.AsString() == GlobalTools.ServerTools.ServerType.JBoss5;
                }
            }
        }
    }
}


